package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.OptionsMapper;
import com.tanhua.model.db.Options;
import com.tanhua.model.vo.OptionsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class OptionsApiImpl implements OptionsApi {

    @Autowired
    private OptionsMapper optionsMapper;

    //获取所有的选项并且组装成map
    @Override
    public Map<String, List<OptionsVo>> findAll() {
        List<Options> options = optionsMapper.selectList(null);
        Map<String, List<OptionsVo>> map = new HashMap();
        for (Options option : options) {
            String questionId = option.getQuestionId().toString();
            OptionsVo vo = OptionsVo.inint(option);
            if (map.containsKey(questionId)){
                List<OptionsVo> list = map.get(questionId);
                list.add(vo);
                map.put(questionId,list);
            }else {
                List<OptionsVo> list2 = new ArrayList<>();
                list2.add(vo);
                map.put(questionId,list2);
            }
        }
        return map;
    }
    //根据options获取总分
    @Override
    public Integer findSumScoreByIds(List<Long> optionIds) {
        QueryWrapper<Options> wrapper = new QueryWrapper<>();
        wrapper.in("id",optionIds);
        List<Options> options = optionsMapper.selectList(wrapper);
        Integer score = 0;
        for (Options option : options) {
            score += option.getScore();
        }
        return score;
    }
}
